package nl.boektrust.btsoep.server.wsdl;

import java.util.List;

import nl.boektrust.btsoep.client.PersistentService;
import nl.boektrust.btsoep.server.PersistentServiceImpl;
import nl.boektrust.btsoep.server.PersistentServiceSoap;
import nl.boektrust.btsoep.server.jaxws.DeletePerson;
import nl.boektrust.btsoep.server.jaxws.DeletePersonResponse;
import nl.boektrust.btsoep.server.jaxws.DeletePhysicalBookManifestation;
import nl.boektrust.btsoep.server.jaxws.DeletePhysicalBookManifestationResponse;
import nl.boektrust.btsoep.server.jaxws.GetAllPersons;
import nl.boektrust.btsoep.server.jaxws.GetAllPersonsResponse;
import nl.boektrust.btsoep.server.jaxws.GetAllPhysicalBookManifestations;
import nl.boektrust.btsoep.server.jaxws.GetAllPhysicalBookManifestationsResponse;
import nl.boektrust.btsoep.server.jaxws.GetBooksByISBN;
import nl.boektrust.btsoep.server.jaxws.GetBooksByISBNResponse;
import nl.boektrust.btsoep.server.jaxws.GetBooksForPerson;
import nl.boektrust.btsoep.server.jaxws.GetBooksForPersonResponse;
import nl.boektrust.btsoep.server.jaxws.GetNumberOfAssets;
import nl.boektrust.btsoep.server.jaxws.GetNumberOfAssetsResponse;
import nl.boektrust.btsoep.server.jaxws.GetNumberOfEntities;
import nl.boektrust.btsoep.server.jaxws.GetNumberOfEntitiesResponse;
import nl.boektrust.btsoep.server.jaxws.GetNumberOfPhysicalBooks;
import nl.boektrust.btsoep.server.jaxws.GetNumberOfPhysicalBooksResponse;
import nl.boektrust.btsoep.server.jaxws.GetPersonsByPartialName;
import nl.boektrust.btsoep.server.jaxws.GetPersonsByPartialNameResponse;
import nl.boektrust.btsoep.server.jaxws.PersistBTEntity;
import nl.boektrust.btsoep.server.jaxws.PersistBTEntityResponse;
import nl.boektrust.btsoep.server.jaxws.PersistPhysicalBookManifestation;
import nl.boektrust.btsoep.server.jaxws.PersistPhysicalBookManifestationResponse;
import nl.boektrust.btsoep.server.wsdl.jaxws.SearchBooks;
import nl.boektrust.btsoep.server.wsdl.jaxws.SearchBooksResponse;
import nl.boektrust.btsoep.shared.model.ImageReferenceTypeCode;
import nl.boektrust.btsoep.shared.model.Person;
import nl.boektrust.btsoep.shared.model.PhysicalBookManifestation;

/**
 * BrowseAdapter adapts original java {@link BrowseWsdl} into request/response
 * suited for SOAP. All the WSGEN generated types of {@link PersistentService}
 * are handled also
 * 
 * @author frans
 * 
 */
public class BrowseAdapter {
	private final BrowseWsdl browseWsdl = new BrowseWsdl();

	/**
	 * Adapts {@link BrowseWsdl#searchBooks(Book, Book, int, int)} into
	 * request/response.
	 * 
	 * @param searchBooks
	 *            input with Arg0..n
	 * @return result List of {@link Book} gest wrapped in response object.
	 */
	public SearchBooksResponse searchBooks(SearchBooks searchBooks) {
		List<Book> ret = browseWsdl.searchBooks(searchBooks.getArg0(),
				searchBooks.getArg1(), searchBooks.getArg2(),
				searchBooks.getArg3());
		SearchBooksResponse searchBooksResponse = new SearchBooksResponse();
		searchBooksResponse.setReturn(ret);
		return searchBooksResponse;
	}

	public GetBooksForPersonResponse getBooksForPerson(GetBooksForPerson gbfp) {
		Person p = gbfp.getArg0();
		PersistentServiceSoap pss = new PersistentServiceSoap();
		PersistentServiceImpl psi = new PersistentServiceImpl();
		pss.setPsi(psi);
		List<PhysicalBookManifestation> l = pss.getBooksForPerson(p);
		GetBooksForPersonResponse ret = new GetBooksForPersonResponse();
		ret.setReturn(l);
		return ret;
	}

	public GetAllPersonsResponse getAllPersons(GetAllPersons getAllPersons) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		PersistentServiceImpl psi = new PersistentServiceImpl();
		pss.setPsi(psi);
		List<Person> lp = pss.getAllPersons();
		GetAllPersonsResponse ret = new GetAllPersonsResponse();
		ret.setReturn(lp);
		return ret;
	}

	public DeletePersonResponse deletePerson(DeletePerson deletePerson) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		Person person = deletePerson.getArg0();
		pss.deletePerson(person);
		DeletePersonResponse deletePersonResponse = new DeletePersonResponse();
		return deletePersonResponse;
	}

	public DeletePhysicalBookManifestationResponse deletePhysicalBookManifestation(
			DeletePhysicalBookManifestation deletePhysicalBookManifestation) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		PhysicalBookManifestation physicalBookManifestation = deletePhysicalBookManifestation
				.getArg0();
		pss.deletePhysicalBookManifestation(physicalBookManifestation);
		DeletePhysicalBookManifestationResponse deletePhysicalBookManifestationResponse = new DeletePhysicalBookManifestationResponse();
		return deletePhysicalBookManifestationResponse;
	}

	public GetAllPhysicalBookManifestationsResponse getAllPhysicalBookManifestations(
			GetAllPhysicalBookManifestations getAllPhysicalBookManifestations) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		pss.getAllPhysicalBookManifestations();
		GetAllPhysicalBookManifestationsResponse getAllPhysicalBookManifestationsResponse = new GetAllPhysicalBookManifestationsResponse();
		return getAllPhysicalBookManifestationsResponse;
	}

	public GetBooksByISBNResponse getBooksByISBN(GetBooksByISBN getBooksByISBN) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		String isbn = getBooksByISBN.getArg0();
		List<PhysicalBookManifestation> l = pss.getBooksByISBN(isbn);
		GetBooksByISBNResponse getBooksByISBNResponse = new GetBooksByISBNResponse();
		getBooksByISBNResponse.setReturn(l);
		return getBooksByISBNResponse;
	}

	PersistPhysicalBookManifestationResponse persistPhysicalBookManifestation(
			PersistPhysicalBookManifestation persistPhysicalBookManifestation) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		PhysicalBookManifestation physicalBookManifestation = persistPhysicalBookManifestation
				.getArg0();
		PersistPhysicalBookManifestationResponse persistPhysicalBookManifestationResponse = new PersistPhysicalBookManifestationResponse();
		pss.persistPhysicalBookManifestation(physicalBookManifestation);
		// persistPhysicalBookManifestation.setArg0(arg0);
		return persistPhysicalBookManifestationResponse;
	}

	PersistBTEntityResponse persistBTEntity(PersistBTEntity persistBTEntity) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		String s1 = persistBTEntity.getArg0();
		String s2 = persistBTEntity.getArg1();
		PersistBTEntityResponse persistBTEntityResponse = new PersistBTEntityResponse();
		pss.persistBTEntity(s1, s2);
		return persistBTEntityResponse;
	}

	GetPersonsByPartialNameResponse getPersonsByPartialName(
			GetPersonsByPartialName getPersonsByPartialName) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		String s = getPersonsByPartialName.getArg0();
		List<Person> lp = pss.getPersonsByPartialName(s);
		GetPersonsByPartialNameResponse getPersonsByPartialNameResponse = new GetPersonsByPartialNameResponse();
		getPersonsByPartialNameResponse.setReturn(lp);
		return getPersonsByPartialNameResponse;
	}

	GetNumberOfPhysicalBooksResponse getNumberOfPhysicalBooks(
			GetNumberOfPhysicalBooks getNumberOfPhysicalBooks) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		int nr = pss.getNumberOfPhysicalBooks();
		GetNumberOfPhysicalBooksResponse getNumberOfPhysicalBooksResponse = new GetNumberOfPhysicalBooksResponse();
		getNumberOfPhysicalBooksResponse.setReturn(nr);
		return getNumberOfPhysicalBooksResponse;
	}

	GetNumberOfEntitiesResponse getNumberOfEntities(
			GetNumberOfEntities getNumberOfEntities) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		GetNumberOfEntitiesResponse getNumberOfEntitiesResponse = new GetNumberOfEntitiesResponse();
		int nr = pss.getNumberOfEntities();
		getNumberOfEntitiesResponse.setReturn(nr);
		return getNumberOfEntitiesResponse;
	}

	GetNumberOfAssetsResponse getNumberOfAssets(
			GetNumberOfAssets getNumberOfAssets) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		ImageReferenceTypeCode imageReferenceTypeCode = getNumberOfAssets
				.getArg0();
		int nr = pss.getNumberOfAssets(imageReferenceTypeCode);
		GetNumberOfAssetsResponse getNumberOfAssetsResponse = new GetNumberOfAssetsResponse();
		getNumberOfAssetsResponse.setReturn(nr);
		return getNumberOfAssetsResponse;
	}

	GetBooksByISBNResponse GetBooksByISBN(GetBooksByISBN getBooksByISBN) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		String isbn = getBooksByISBN.getArg0();
		List<PhysicalBookManifestation> lpbm = pss.getBooksByISBN(isbn);
		GetBooksByISBNResponse getBooksByISBNResponse = new GetBooksByISBNResponse();
		getBooksByISBNResponse.setReturn(lpbm);
		return getBooksByISBNResponse;
	}

	GetAllPhysicalBookManifestationsResponse GetAllPhysicalBookManifestations(
			GetAllPhysicalBookManifestations getAllPhysicalBookManifestations) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		List<PhysicalBookManifestation> lpbm = pss
				.getAllPhysicalBookManifestations();
		GetAllPhysicalBookManifestationsResponse getAllPhysicalBookManifestationsResponse = new GetAllPhysicalBookManifestationsResponse();
		getAllPhysicalBookManifestationsResponse.setReturn(lpbm);
		return getAllPhysicalBookManifestationsResponse;
	}

	DeletePhysicalBookManifestationResponse DeletePhysicalBookManifestation(
			DeletePhysicalBookManifestation deletePhysicalBookManifestation) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		PhysicalBookManifestation book = deletePhysicalBookManifestation
				.getArg0();
		pss.deletePhysicalBookManifestation(book);
		DeletePhysicalBookManifestationResponse deletePhysicalBookManifestationResponse = new DeletePhysicalBookManifestationResponse();
		return deletePhysicalBookManifestationResponse;
	}

	DeletePersonResponse DeletePerson(DeletePerson deletePerson) {
		PersistentServiceSoap pss = new PersistentServiceSoap();
		pss.setPsi(new PersistentServiceImpl());
		Person person = deletePerson.getArg0();
		pss.deletePerson(person);
		DeletePersonResponse deletePersonResponse = new DeletePersonResponse();
		return deletePersonResponse;
	}

}
